package com.mf.ergate.controller.auth;

import com.mf.ergate.api.auth.RoleUserRelationRemoteService;
import com.mf.ergate.api.auth.model.dto.RoleUserRelationCreateDto;
import com.mf.ergate.api.auth.model.dto.RoleUserRelationDeleteDto;
import com.mf.ergate.api.auth.model.dto.RoleUserRelationQueryDto;
import com.mf.ergate.api.auth.model.dto.RoleUserRelationUpdateDto;
import com.mf.ergate.api.auth.model.vo.RoleUserRelationVo;
import com.mf.ergate.common.utils.MfBeanUtils;
import com.mf.ergate.domain.model.auth.RoleUserRelationEntity;
import com.mf.ergate.service.auth.RoleUserRelationService;
import com.mf.framework.common.model.Page;
import com.mf.framework.common.model.RemotePage;
import com.mf.framework.common.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户与角色关联Controller
 * User: zhaoming
 * DateTime: 2019-02-08
 * To change this template use File | Settings | File Templates.
 */
@RestController
public class RoleUserRelationController implements RoleUserRelationRemoteService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RoleUserRelationService roleUserRelationService;

    /**
     * 新增
     * @param roleUserRelationCreateDto 用户与角色关联新增Dto
     * @return 用户与角色关联
     */
    public Result<RoleUserRelationVo> insert(@RequestBody RoleUserRelationCreateDto roleUserRelationCreateDto) {
        RoleUserRelationEntity roleUserRelationEntity = MfBeanUtils.copyObject(roleUserRelationCreateDto, RoleUserRelationEntity.class);
        roleUserRelationEntity = roleUserRelationService.insert(roleUserRelationEntity);
        RoleUserRelationVo roleUserRelationVo = MfBeanUtils.copyObject(roleUserRelationEntity, RoleUserRelationVo.class);
        return new Result<>(roleUserRelationVo);
    }

    /**
     * 根据唯一编号更新
     * @param roleUserRelationUpdateDto 用户与角色关联更新Dto
     * @return 执行结果
     */
    public Result<Boolean> updateByUniqueNo(@RequestBody RoleUserRelationUpdateDto roleUserRelationUpdateDto) {
        RoleUserRelationEntity roleUserRelationEntity = MfBeanUtils.copyObject(roleUserRelationUpdateDto, RoleUserRelationEntity.class);
        Boolean flag = roleUserRelationService.updateByUniqueNo(roleUserRelationEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号删除(逻辑删除)
     * @param roleUserRelationDeleteDto 用户与角色关联删除Dto
     * @return 结果集
     */
    public Result<Boolean> deleteByUniqueNo(@RequestBody RoleUserRelationDeleteDto roleUserRelationDeleteDto){
        RoleUserRelationEntity roleUserRelationEntity = MfBeanUtils.copyObject(roleUserRelationDeleteDto, RoleUserRelationEntity.class);
        Boolean flag = roleUserRelationService.deleteByUniqueNo(roleUserRelationEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号查询
     * @param uniqueNo 唯一编号
     * @return 用户与角色关联
     */
    public Result<RoleUserRelationVo> selectByUniqueNo(@RequestBody String uniqueNo) {
        RoleUserRelationEntity roleUserRelationEntity = roleUserRelationService.selectByUniqueNo(uniqueNo);
        RoleUserRelationVo roleUserRelationVo = MfBeanUtils.copyObject(roleUserRelationEntity, RoleUserRelationVo.class);
        return new Result<>(roleUserRelationVo);
    }

    /**
     * 根据条件查询
     * @param roleUserRelationQueryDto 用户与角色关联查询Dto
     * @return 用户与角色关联列表
     */
    public Result<List<RoleUserRelationVo>> selectByCondition(@RequestBody RoleUserRelationQueryDto roleUserRelationQueryDto) {
        List<RoleUserRelationEntity> roleUserRelationEntities = roleUserRelationService.selectByCondition(roleUserRelationQueryDto);
        List<RoleUserRelationVo> roleUserRelationVos = MfBeanUtils.copyList(roleUserRelationEntities, RoleUserRelationVo.class);
        return new Result<>(roleUserRelationVos);
    }

    /**
     * 根据条件查询数量
     * @param roleUserRelationQueryDto 用户与角色关联查询Dto
     * @return 数量
     */
    public Result<Integer> selectCount(@RequestBody RoleUserRelationQueryDto roleUserRelationQueryDto) {
        return new Result<>(roleUserRelationService.selectCount(roleUserRelationQueryDto));
    }

    /**
     * 根据条件分页查询
     * @param remotePage 远程分页对象
     * @return 用户与角色关联分页列表
     */
    public Result<Page<RoleUserRelationVo>> selectPage(@RequestBody RemotePage<RoleUserRelationQueryDto> remotePage){
        Page<RoleUserRelationEntity> roleUserRelationEntityPage = roleUserRelationService.selectPage(remotePage.getPage(), remotePage.getObj());
        Page<RoleUserRelationVo> page = MfBeanUtils.copyPage(roleUserRelationEntityPage, RoleUserRelationVo.class);
        return new Result<>(page);
    }

}